package com.rs.service.impl.rs;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rs.dao.AskforMapper;
import com.rs.dao.AttendanceMapper;
import com.rs.dao.AttendanceRulesMapper;
import com.rs.model.Askfor;
import com.rs.model.Attendance;
import com.rs.model.AttendanceRules;
import com.rs.service.rs.AskforSercive;
import com.rs.util.TimerTest;
@Service
public class AskforSerciveImpl implements AskforSercive {
	@Autowired
	private AskforMapper afm;
	@Autowired
	private AttendanceMapper am;
	@Autowired
	private AttendanceRulesMapper arDao;
	@Override
	public void askforAdd(Askfor askfor) {
		// TODO Auto-generated method stub
		afm.insert(askfor);
	}

	@Override
	public List<Askfor> askforSelectAll() {
		// TODO Auto-generated method stub
		return afm.askforselectAll();
	}

	@Override
	public List<Askfor> askforSelectByTiaojian(Askfor askfor) {
		// TODO Auto-generated method stub
		return afm.askforSelectByTiaojian(askfor);
	}

	@Override
	public void askforUpDate(Askfor askfor) {
		// TODO Auto-generated method stub
		afm.updateByPrimaryKey(askfor);
	}

	@Override
	public Askfor askforSelectById(int ask_id) {
		// TODO Auto-generated method stub
		return afm.selectByPrimaryKey(ask_id);
	}

	@Override
	public void askforDel(int ask_id) {
		// TODO Auto-generated method stub
		afm.selectByPrimaryKey(ask_id);
	}

	@Override
	public void askforToExamine(Askfor askfor) {
		// TODO Auto-generated method stub
		//创建一个打卡记录的实体类
		Attendance aa = new Attendance();
		//创建四个打卡状态
		int at_amtype = 0;
		int at_noontype = 0;
		int at_pmtype = 0;
		int at_nighttype = 0;
		//查看打卡规则
		AttendanceRules ra = arDao.rarSelectAll();
		//为打卡状态赋值
		if(ra.getAtru_am().equals("No clock in")){
			at_amtype = -1;
		}else {
			at_amtype = 6;
		}
		if(ra.getAtru_noon().equals("No clock in")){
			at_noontype = -1;
		}else {
			at_noontype = 6;
		}
		if(ra.getAtru_pm().equals("No clock in")){
			at_pmtype = -1;
		}else {
			at_pmtype = 6;
		}
		if(ra.getAtru_night().equals("No clock in")){
			at_nighttype = -1;
		}else {
			at_nighttype = 6;
		}
		//获取请假是否通过
		int a = askfor.getAsk_audit();
		//获取请假记录的主键
		int ask_id = askfor.getAsk_id();
		//如果请假不通过，修改状态
			if(a==2) {
				afm.updateByPrimaryKey(askfor);
				return;
			}
		//如果请假通过新增或修改打卡记录
			else{
					afm.updateByPrimaryKey(askfor);//修改状态
					Askfor a1 = afm.selectByPrimaryKey(ask_id);//查看这条请假记录
					//获取请假的时间节点 
					String ask_ktimes = a1.getAsk_ktime()+"";
					String ask_jtimes = a1.getAsk_jtime()+"";
					//获取请假天数
					double ask_days = a1.getAsk_days();
					//调用工具类获取休息的日期
					TimerTest tt = new TimerTest();
					try {
						//获取休息日期的集合
						List<String> ll = tt.dateNumber(a1);
						//新增空的打卡记录
						for(String l1:ll){
							aa.setAt_uid(a1.getAsk_uid());
							aa.setAt_days(l1);
							Attendance a2 = am.attendanceSelectByDay(aa);
							if(null!=a2) {
								break;
							}else{
								am.attendanceAdd1(aa);
							}
						}
						//根据请假日期更改打卡记录
						for(String l1:ll){
							aa=null;
							aa.setAt_uid(a1.getAsk_uid());
							aa.setAt_days(l1);
//							Attendance a2 = am.attendanceSelectByDay(aa);
//							if(null!=a2){
								if(ask_ktimes.equals(l1+" 07:00:00")){
									if(ask_days<1){
										aa.setAt_amtype(at_amtype);
										aa.setAt_noontype(at_noontype);
										am.attendanceUpDate1(aa);
									}else if(ask_days>=1){
										aa.setAt_amtype(at_amtype);
										aa.setAt_noontype(at_noontype);
										aa.setAt_pmtype(at_pmtype);
										aa.setat_nighttype(at_nighttype);
										am.attendanceUpDate1(aa);
									}
								}else if(ask_ktimes.equals(l1+" 13:00:00")){
										aa.setAt_pmtype(at_pmtype);
										aa.setat_nighttype(at_nighttype);
										am.attendanceUpDate1(aa);
								}else{
									if(ask_jtimes.equals(l1+" 13:00:00")){
										aa.setAt_amtype(at_amtype);
										aa.setAt_noontype(at_noontype);
										am.attendanceUpDate1(aa);
									}else{
										aa.setAt_amtype(at_amtype);
										aa.setAt_noontype(at_noontype);
										aa.setAt_pmtype(at_pmtype);
										aa.setat_nighttype(at_nighttype);
										am.attendanceUpDate1(aa);
									}
								}        
//						    }
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
	}

	
}
